package org.vectrics.user;

import java.sql.ResultSet;
import java.util.HashSet;
import java.util.Set;
import org.vectrics.common.db.JdbcTarget;
import org.vectrics.common.db.ProcessException;

public class FindCommonFirstNamesTarget implements JdbcTarget {
	private boolean startsWith = false;
	private String firstName = null;
	private Set matches = new HashSet();

	public FindCommonFirstNamesTarget(String firstName, boolean startsWith) {
		super();
		this.startsWith = startsWith;
		this.firstName = firstName;
	}

	public void processResults(ResultSet resultSet) throws ProcessException {
		try {
			while (resultSet.next()) {
				matches.add(resultSet.getString("name"));
			}
		} catch (Exception ex) {
			throw new ProcessException("Exception finding results", ex);
		}
	}
	
	public Set getMatches() {
		return(matches);
	}

	public String getQuery() {
		String query = null;
		if (startsWith) {
			query = "select name from cmn_first_name where (name like " +
					"'" + firstName + "%')";
		} else {
			query = "select name from cmn_first_name where (name = " +
			"'" + firstName + "')";
			
		}
		return query;
	}

}
